import _ from "lodash";
import {
    fittingOriginOptions, 
    masuriumOptions, 
    srDictKeyMap, 
    userStarRatingOptions
} from "@constants";
/**
 * 接口字段转换
 * @param {*} list
 * @param {*} transferConfig
 * @returns Array
 */
const transfer = (list, transferConfig) => {
    return (list ||[]).map(item => {
        let temp = {...item};
        Object.keys(transferConfig).forEach(key => {
            if(item[key] || item[key] === 0) {
                let configItem = transferConfig[key];
                if(configItem.transferMap && typeof configItem.transferMap === 'object' && configItem.output) {

                    let value = configItem.transferMap[item[key]];
                    let newKey = configItem.output;

                    temp[newKey] = value;

                }
            }
        })
        return temp;
    });
}

export const responseFormHandle_auxiliaryTool_customerAdvisory = (
    response = {}
) => {
    return {
        message: "",
        sucess: "0000",
        data: response.rsp.data.tollStationList,
    };
};

export const responseFormHandle_auxiliaryTool_customerAdvisory_form = (
    response = {}
) => {
    return {
        message: "",
        sucess: "0000",
        data: response.rsp.data,
    };
};

export const responseFormHandle_msgresult = (response = {}) => {
    let tempResponse = { ...response };
    let message = response.msg;
    let sucess = response.result === "0000";

    delete tempResponse.msg;
    delete tempResponse.result;

    let data = tempResponse;

    return {
        message,
        sucess,
        data,
    };
};

export const responseFormHandle_sr = (response = {}) => {
    let tempResponse = { ...(response.rsp || response || {}) };
    let message = tempResponse.rspDesc || response?.msg;
    // let sucess = [null, "0", "1"].indexOf(tempResponse.rspCode) !== -1;
    let sucess = ["0", null, '1'].indexOf(tempResponse.rspCode) !== -1;


    delete tempResponse.rspDesc;
    delete tempResponse.rspCode;

    let data = tempResponse;

    return {
        message,
        sucess,
        data,
    };
};
//新增
export const responseFormHandle_task = (response = {}) => {
    let tempResponse = { ...response };
    let message = response.msg;
    let sucess = response.code == "200";

    delete tempResponse.msg;
    delete tempResponse.code;

    let data = tempResponse;

    return {
        message,
        sucess,
        data,
    };
};
//新增
export const responseFormHandle_user = (response = {}) => {
    let tempResponse = { ...response };
    let message = response.rspDesc;
    let sucess = response.rspCode == "200";

    delete tempResponse.rspDesc;
    delete tempResponse.rspCode;

    let data = tempResponse;

    return {
        message,
        sucess,
        data,
    };
};

// 台账接口处理
export const responseFormHandle_boot = (response = {}) => {
    let tempResponse = { ...(response || {}) };
    let message = tempResponse.Msg;
    let sucess = ["0000"].indexOf(tempResponse.status) !== -1;

    let data = tempResponse;

    return {
        message,
        sucess,
        data,
    };
};

export const responseFormHandle_zsk_sr = (response = {}) => {
    let tempResponse = { ...(response.rsp || {}) };
    let message = tempResponse.RSP_DESC;
    let sucess = ["0", null, "1"].indexOf(tempResponse.RSP_CODE) !== -1;

    let data = tempResponse;
    return {
        message,
        sucess,
        data,
    };
};

export const responseFormHandle_OrderExport = (data, response) => {
    let contentDisposition = response.headers['content-disposition'];
    let fileName = '';
    contentDisposition && contentDisposition.split(';').forEach(item => {
        let arr = item.split('=');
        if(arr[0] === 'filename' && arr[1]) {
            fileName = arr[1];
        }
    });
    return {
        sucess: Boolean(data),
        data: {
            data: data,
            fileName: fileName && decodeURIComponent(fileName)
        },
        message: ''
    };
}

export const responseFormHandle_zsk = (response = {}) => {
    let tempResponse = { ...(response || {}) };
    let message = _.get(tempResponse, "RSP.RSP_DESC") || tempResponse.MSG ||tempResponse.msg;
    let sucess =
        tempResponse.STATUS === "0000" &&
        ["0000", "1", "0","3", null].includes(_.get(tempResponse, "RSP.RSP_CODE"));

    let data = tempResponse.RSP;

    return {
        message,
        sucess,
        data,
    };
};

export const responseFormHandle_yw = (response = {}) => {
    let tempResponse = { ...(response || {}) };
    let message = _.get(tempResponse, "RSP.RSP_DESC") || tempResponse.MSG ||tempResponse.msg;
    let sucess = tempResponse.result==='0'

    let data = tempResponse.rows;

    return {
        message,
        sucess,
        data,
    };
};

export const responseFormHandle_zsk_tags = (response = {}) => {
    let tempResponse = { ...(response || {}) };
    let message = _.get(tempResponse, "RSP_DESC") || tempResponse.MSG;
    let sucess = _.get(tempResponse, "RSP_CODE") === "1";

    let data = tempResponse;

    return {
        message,
        sucess,
        data,
    };
};

export const responseFormHandle_gzz = (response = {}) => {
    let tempResponse = { ...(response || {}) };
    let message = _.get(tempResponse, "RSP_DESC") || tempResponse.MSG;
    let sucess = _.get(tempResponse, "RSP_CODE") === "0";

    let data = tempResponse.DATA;

    return {
        message,
        sucess,
        data,
    };
};

export const responseFormHandle_zsk_bak = (response = {}) => {
    let tempResponse = { ...(response || {}) };
    let message = _.get(tempResponse, "RSP.RSP_DESC") || tempResponse.MSG ||tempResponse.rspMsg;
    let sucess =
        tempResponse.STATUS === "0000" ||
        _.get(tempResponse, "RSP.RSP_CODE") === "1" ||
        _.get(tempResponse, "RSP.RSP_CODE") === "0000" ||
				_.get(tempResponse, "RSP.RSP_CODE") === "2"||
                _.get(tempResponse, "rspCode") === "0";

    let data = tempResponse.RSP;

    return {
        message,
        sucess,
        data,
    };
};
export const responseFormHandle_aiqm = (response = {}) => {
    let tempResponse = { ...(response || {}) };
    let message = _.get(tempResponse, "RSP.RSP_DESC") || tempResponse.MSG ||tempResponse.rspMsg;
    let sucess =
        tempResponse.STATUS === "0000" ||
        _.get(tempResponse, "RSP.RSP_CODE") === "1" ||
        _.get(tempResponse, "RSP.RSP_CODE") === "0000" ||
				_.get(tempResponse, "RSP.RSP_CODE") === "2"||
                _.get(tempResponse, "rspCode") === "0";

    let data = tempResponse.RSP?.DATA[0];

    return {
        message,
        sucess,
        data,
    };
};

export const srPageination_responseDataFormat = (responseData) => {
    let list = [];
    let data = null;
    if(responseData.data && Object.prototype.toString.call(responseData.data) === "[object Array]") {
        list = responseData.data;
    }else if(responseData.date && Object.prototype.toString.call(responseData.date) === "[object Array]") {
        list = responseData.date;
    }else {
        list = responseData.datas;
        data = responseData.data;
    }
    let total = _.get(responseData, "attach.total")
    if(total){
        total = Number(total)
    }

    return {
        list: list,
        result:_.get(responseData, "result"),
        total: total || Number(responseData.count) || Number(responseData.total),
        data
    };
};

export const srPageination_responseDataFormat_two = (responseData) => {
    let list = [];
    let data = null;
    if(responseData.data && Object.prototype.toString.call(responseData.data) === "[object Array]") {
        list = responseData.data;
    }else {
        list = responseData.datas;
        data = responseData.data;
    }
    return {
        list: list,
        result:_.get(responseData, "result"),
        total: _.get(responseData, "attach.total"),
        data
    };
};

export const new_srPageination_responseDataFormat = (responseData) => {
   
    responseData = responseData.rsp;
    let list = [];
    let data = null;
    if (
        responseData.data &&
        Object.prototype.toString.call(responseData.data) === "[object Array]"
    ) {
        list = responseData.data;
    } else {
        list = responseData.datas;
        data = responseData.datas;
    }
    return {
        list: list,
        total: _.get(responseData, "attach.total"),
        data,
        sucess: responseData.rspCode,
    };
};

export const accessMonitor_responseDataFormat = (responseData) => {
    let list = [];
    let data = null;
    if (
        responseData.data &&
        Object.prototype.toString.call(responseData.data) === "[object Array]"
    ) {
        list = responseData.data;
    } else {
        list = responseData.data;
        data = responseData.datas;
    }
    return {
        list: list,
        total: _.get(responseData, "attach.total"),
        data,
    };
};
export const responseFuntion = (responseData) => { //培训表格分页
  
    let data=responseData?.pagination?.rows;
    return {
        list: data,
        total: _.get(responseData, "pagination.total"),
        sucess: responseData.code,
        message: responseData.message,
    };
};
export const responseFuntion_outcall = (responseData) => { //外呼表格分页
    let data=responseData?.rows;
    let sucess=null
    if(data.length>=0){ //针对没有状态返回情况，简易处理。
        sucess='0000'
    }
    return {
        list: data,
        total:responseData.total,
        sucess,
        message:responseData.msg
    };
};
export const defaultResponseFuntion = (responseData) => {
    return {
        data: responseData,
        sucess: responseData.code,
        message: responseData.message,
    };
};
export const responseFuntion_notable = (responseData) => {
    //非表格

    let data = responseData?.pagination?.rows;
    return {
        list: data,
        total: _.get(responseData, "pagination.total"),
        sucess: responseData.code,
        message: responseData.message,
    };
};
//工单时限配置要素
export const selectCfgTmlmtConfig_responseDataFormat = (responseData) => {
    responseData = srPageination_responseDataFormat(responseData);
    (responseData.list||[]).forEach(item=>{
        masuriumOptions.forEach(value => {
            if (item.element==value.key){
                item.elementNm=value.label
            }
        })

    })

    return responseData;
};

//清除数组中 对象key值为null的字段
export const selectAgentToShift_responseDataFormat = (responseData) => {
    responseData = srPageination_responseDataFormat(responseData);
    responseData.list = responseData.list.filter(item => {
        return item.staffName && item.staffId
    })
    return responseData;
};

export const zskPageination_responseDataFormat = (responseData) => {
    return {
        list: responseData.DATA,
        total: _.get(responseData, "ATTACH.TOTAL"),
    };
};
export const aiqm_responseDataFormat = (responseData) => {
    return {
        list: responseData.jsonArray,
        total: _.get(responseData, "totalAll"),
    };
};
export const billListResponseDataFormat = (data) => {
    const transferConfig = {
        type: {
            output: '$_type',
            transferMap: {
                '1': '扫码支付',
                '2': '领卡代扣',
                '3': '不停车代扣',
                '4': 'ETC储值卡代扣',
            }
        },
        exitVehicleFlag: {
            output: '$_exitVehicleFlag',
            transferMap: {
                '1': '客车',
                '2': '货车',
            }
        },
        payType: {
            output: '$_payType',
            transferMap: {
                "01": '微信',
                "02": '支付宝',
                "03": '银联',
            }
        },
        payStatus: {
            output: '$_payStatus',
            transferMap: {
                '1': '成功',
                '2': '失败',
                '3': '已撤单/已退款',
            }
        }
    }

    if(Array.isArray(data.data)) {
        data.data = transfer(data.data, transferConfig);
    }else if(data.data&&data.data.billList!==undefined) {
        data.data.billList = transfer(data.data.billList, transferConfig);
    }
    return data;
}

export const responseFormHandle_sam = (response = {}) => {

    let tempResponse = { ...(response || {}) };
    let message = tempResponse.retMessage;
    let retCode = _.get(tempResponse, 'retCode')
    let sucess = retCode ? String(retCode) === '200' : Boolean(response) ;

    delete tempResponse.rspDesc;
    delete tempResponse.rspCode;

    let data = tempResponse;
    return {
        message,
        sucess,
        data,
    };
};

export const responseFormHandle_sam_staff = (response = {}) => {

    let tempResponse = { ...(response || {}) };
    let message = tempResponse.resultMsg;
    let retCode = _.get(tempResponse, 'resultVal')
    let sucess = retCode ? String(retCode) === '1' : Boolean(response) ;

    let data = tempResponse;
    return {
        message,
        sucess,
        data,
    };
};

export const responseFormHandle_resultMsg = (response = {}) => {
    let tempResponse = { ...(response || {}) };
    let message = tempResponse.resultMsg;
    let retCode = _.get(tempResponse, "resultVal", "")+"";
    let sucess = retCode ? String(retCode) === "1" : Boolean(response);

    let data = tempResponse.resultMsg;
    return {
        message,
        sucess,
        data,
    };
};

export const responseFormHandle_sam_tree = (response = {}) => {
    let data = typeof response === 'string' ? [] : response;
    let message = typeof response === 'string' ? response : '';
    let sucess = typeof response === 'string' ? false : true;
    return {
        message,
        sucess,
        data,
    };
}

export const responseFormHandle_result = (response = {}) => {
    return {
        message: response.message,
        sucess: response.result === "success",
        response,
    };
};

export const samPageination_responseDataFormat = (responseData) => {
    return {
        list: responseData.rows || responseData.list || responseData.beans,
        total: _.get(responseData, "total"),
    };
};

export const responseFormHandle_draw = (response = {}) => {
    const flag = Boolean(response);
    return {
        sucess: flag,
        message: flag ? "" : response,
        data: response,
    };
};
export const downloadFile=(res,fileName)=> { 
    
    if (!res) {
        return
    }
    if (window.navigator.msSaveBlob) { 
        try {
            window.navigator.msSaveBlob(res, fileName) 
        } catch (e) {
            console.log(e)
        }
    } else {
        let url = window.URL.createObjectURL(new Blob([res], {
            type: 'application/vnd.ms-excel;charset=uft-8'
        }));
        let link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', 'excel文件') 
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link) // 下载完成移除元素
        window.URL.revokeObjectURL(url) // 释放掉blob对象
    }
  }
export const responseFormHandle_asradapter = (response = {}) => {
    let tempResponse = { ...(response || {}) };
    let message = tempResponse.message;
    // let retCode = _.get(tempResponse, 'retCode')
    let sucess = tempResponse.success
        ? tempResponse.success === true
        : Boolean(response);

    delete tempResponse.rspDesc;
    delete tempResponse.rspCode;

    let data = tempResponse;
    return {
        message,
        sucess,
        data,
    };
};

export const asradapterPageination_responseDataFormat = (responseData) => {
    return {
        list: responseData.rows || responseData.list,
        total: _.get(responseData, "total"),
    };
};

export const responseFormHandle_monitor = (response = {}) => {
    console.log(response, "responseresponse");
    // console.log(response);
    // let tempResponse = { ...(response || {}) };
    // let message =
    //     tempResponse.message === "success" || tempResponse.message === "sucess"
    //         ? "成功"
    //         : tempResponse.message;
    // let retCode = _.get(tempResponse, "code");
    // let sucess = retCode ? String(retCode) === "0000" : Boolean(response);

    // delete tempResponse.rspDesc;
    // delete tempResponse.rspCode;

    // let data = tempResponse.pagination;
    // return {
    //     // message,
    //     sucess,
    //     data,
    // };

    let tempResponse = { ...response };
    let message = response.message;
    let sucess = response.code === "0000";

    delete tempResponse.msg;
    delete tempResponse.result;

    let data = tempResponse;
    return {
        message,
        sucess,
        data,
    };
};

export const responseFormHandle_monitor_error = (response = {}) => {
    // console.log(response);
    // let tempResponse = { ...(response || {}) };
    // let message =
    //     tempResponse.message === "success" || tempResponse.message === "sucess"
    //         ? "成功"
    //         : tempResponse.message;
    // let retCode = _.get(tempResponse, "code");
    // let sucess = retCode ? String(retCode) === "0000" : Boolean(response);

    // delete tempResponse.rspDesc;
    // delete tempResponse.rspCode;

    // let data = tempResponse.pagination;
    // return {
    //     // message,
    //     sucess,
    //     data,
    // };

    let tempResponse = { ...response };
    let message = response.msg;
    let sucess = response.result === "0000";

    delete tempResponse.msg;
    delete tempResponse.result;

    let data = tempResponse;
    return {
        message,
        sucess,
        data,
    };
};
export const monitorPageination_responseDataFormat = (responseData) => {
    return {
        list: responseData.pagination.rows,
        total: _.get(responseData.pagination, "total"),
    };
};

export const responseFormHandle_rsp = (response = {}) => {
    return {
        message: response.msg,
        sucess: String(response.status) === "0000",
        data: response.rsp,
    };
};

export const responseFormHandle_json = (data) => {
    return {
        sucess: data.length > 0,
        data: data,
    };
};

export const responseFormHandle_RSP = (response = {}) => {
    let tempResponse = { ...(response || {}) };
    let message = _.get(tempResponse, "RSP.RSP_DESC") || tempResponse.MSG;
    let sucess =
        tempResponse.STATUS === "0000" ||
        _.get(tempResponse, "RSP.RSP_CODE") === "1" ||
        _.get(tempResponse, "RSP.RSP_CODE") === "0000";

    let data = tempResponse.RSP;

    return {
        message,
        sucess,
        data,
    };
};

export const jsonPageination_responseDataFormat = (responseData) => {
    const data = responseData.DATA[0] || {};
    const list = data?.jsonArray || responseData.DATA || [];
    const total = data?.totalAll || 0;
    return {
        list,
        total,
    };
};

export const responseFormHandle_aioutbound = (response = {}) => {
    let tempResponse = { ...(response || {}) };
    let message = _.get(tempResponse, "RSP_DESC") || tempResponse.MSG;
    let sucess = _.get(tempResponse, "RSP_CODE") === "0";

    let data = tempResponse;

    return {
        message,
        sucess,
        data,
    };
};

export const responseFormHandle_sms = (response = {}) => {
    let tempResponse = { ...(response || {}) };
    let message = tempResponse.msg || tempResponse.RSP_DESC;
    let sucess = ["0"].includes(tempResponse.result);

    let data = tempResponse.object;
    return {
        message,
        sucess,
        data,
    };
}

export const responseFormHandle_rspNull=(response={})=>{
    if (!response.rsp){
        response.rsp={}
    }
    response={
        ...response,
        sucess:response.status == "0000",
        data:response.rsp,
        message: response.msg,
    }
    return response
}

export const httpCode_responseFormHandle = (response={}) => {
    response={
        sucess: response.httpCode === 200,
        message: response.message,
        data: response,
    }
    return response
}